package com.atguigu.rabbit.producer.test;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.UUID;

@SpringBootTest
public class Producer04Direct {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void test01() {
        rabbitTemplate.convertAndSend("direct_exchange", "info", "hello direct exchange....");
    }

    @Test
    public void test02() {

        // 在发送消息之前，将消息的内容写入到数据库中，并且为消息分配一个唯一的id，通过CorrelationData对象进行封装
        String msgId = UUID.randomUUID().toString().replace("-", "");
        CorrelationData correlationData = new CorrelationData(msgId);

        rabbitTemplate.convertAndSend(
                "direct_exchange",
                "error",
                "hello direct exchange....",
                correlationData
        );
    }

    @Test
    public void test03() {

        for (int i = 0; i < 100; i++) {
            // 在发送消息之前，将消息的内容写入到数据库中，并且为消息分配一个唯一的id，通过CorrelationData对象进行封装
            String msgId = UUID.randomUUID().toString().replace("-", "");
            CorrelationData correlationData = new CorrelationData(msgId);

            rabbitTemplate.convertAndSend(
                    "direct_exchange",
                    "error",
                    "hello direct exchange....",
                    correlationData
            );
        }
    }

    @Test
    public void test04() {

        // 在发送消息之前，将消息的内容写入到数据库中，并且为消息分配一个唯一的id，通过CorrelationData对象进行封装
        String msgId = UUID.randomUUID().toString().replace("-", "");
        CorrelationData correlationData = new CorrelationData(msgId);

        rabbitTemplate.convertAndSend(
                "direct_exchange",
                "error",
                "创建订单成功...." + new Date(),
                correlationData
        );
    }
}